package com.snowRemovingUser.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.snowRemovingUser.domain.TPrediction;
import com.snowRemovingUser.domain.TRoadManage;
import com.snowRemovingUser.dto.OriginVo;
import com.snowRemovingUser.dto.PredictionTable;
import com.snowRemovingUser.dto.PredictionVo;
import com.snowRemovingUser.mapper.TPredictionMapper;
import com.snowRemovingUser.service.ITPredictionService;
import com.snowRemovingUser.service.ITRoadManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 预测报表Service业务层处理
 *
 * @author lg
 * @date 2024-04-08
 */
@Service
public class TPredictionServiceImpl implements ITPredictionService
{
    @Autowired
    private TPredictionMapper tPredictionMapper;

    @Autowired
    private ITRoadManageService roadManageService;

    /**
     * 查询预测报表
     *
     * @param id 预测报表主键
     * @return 预测报表
     */
    @Override
    public TPrediction selectTPredictionById(Long id)
    {
        return tPredictionMapper.selectTPredictionById(id);
    }

    /**
     * 查询预测报表列表
     *
     * @param tPrediction 预测报表
     * @return 预测报表
     */
    @Override
    public List<TPrediction> selectTPredictionList(TPrediction tPrediction)
    {
        return tPredictionMapper.selectTPredictionList(tPrediction);
    }

    /**
     * 新增预测报表
     *
     * @param tPrediction 预测报表
     * @return 结果
     */
    @Override
    public int insertTPrediction(TPrediction tPrediction)
    {
        return tPredictionMapper.insertTPrediction(tPrediction);
    }

    /**
     * 修改预测报表
     *
     * @param tPrediction 预测报表
     * @return 结果
     */
    @Override
    public int updateTPrediction(TPrediction tPrediction)
    {
        tPrediction.setUpdateTime(DateUtils.getNowDate());
        return tPredictionMapper.updateTPrediction(tPrediction);
    }

    /**
     * 批量删除预测报表
     *
     * @param ids 需要删除的预测报表主键
     * @return 结果
     */
    @Override
    public int deleteTPredictionByIds(Long[] ids)
    {
        return tPredictionMapper.deleteTPredictionByIds(ids);
    }

    /**
     * 删除预测报表信息
     *
     * @param id 预测报表主键
     * @return 结果
     */
    @Override
    public int deleteTPredictionById(Long id)
    {
        return tPredictionMapper.deleteTPredictionById(id);
    }


    /**
     * 报表基本信息
     * @return
     */
    @Override
    public Map<String, Object> latestInfo(String road) {


        Map<String, Object> predictionMap = new HashMap<>();
        List<PredictionVo> predictionVoList = new ArrayList<>();


        // 查询路段管理表
        TRoadManage tRoadManage3 = new TRoadManage();
        tRoadManage3.setPlace(road);
        List<TRoadManage> tRoadManages1 = roadManageService.selectTRoadManageList(tRoadManage3);
        TRoadManage tRoadManage4 = tRoadManages1.get(0);

        TPrediction tPrediction1 = new TPrediction();
        tPrediction1.setPlaceName(tRoadManage4.getPlace());
        List<TPrediction> tPredictions = tPredictionMapper.selectTPredictionList(tPrediction1);
        List<TPrediction> roadDataList = tPredictions.stream() .filter(prediction -> prediction.getParentId() == 0).collect(Collectors.toList());

        predictionMap.put("roadName", tRoadManage4.getPlaceName());

        predictionMap.put("road", tRoadManage4.getPlace());

        for (TPrediction tPrediction : roadDataList) {


            TRoadManage tRoadManage = new TRoadManage();
            tRoadManage.setPlace(tPrediction.getPlaceName());
            List<TRoadManage> tRoadManages = roadManageService.selectTRoadManageList(tRoadManage);
            TRoadManage tRoadManage1 = tRoadManages.get(0);

            String placeName = tRoadManage1.getPlaceName();

            PredictionVo predictionVo = new PredictionVo();
            predictionVo.setComment(placeName+": "+tPrediction.getComment());
            predictionVo.setPredictionComment(placeName+":"+tPrediction.getPredictionComment());



            PredictionTable predictionTable = new PredictionTable();
            predictionTable.setWeatherExplain(placeName+": "+tPrediction.getWeatherExplain());

            // 表格横向表头
            List<String> transTitle = new ArrayList<>();
            transTitle.add(placeName);

            // 查询儿子
            List<TPrediction> filteredPredictions = tPredictions.stream()
                    .filter(prediction -> prediction.getParentId().equals(tPrediction.getId()) && prediction.getPlaceName().equals(tPrediction.getPlaceName()))
                    .collect(Collectors.toList());

            // 表格纵向表头
            List<String> originTitle = new ArrayList<>();
            // originTitle.add("降雪量");
            originTitle.add("降雨量");
            TRoadManage tRoadManage2 = new TRoadManage();
            tRoadManage2.setParentId(tRoadManage1.getId());
            List<TRoadManage> tRoadManageChildList = roadManageService.selectTRoadManageList(tRoadManage2);
            for (TRoadManage roadManage : tRoadManageChildList) {
                originTitle.add("温度("+ roadManage.getPlace().split("_")[1] + ")");
            }
            predictionTable.setOriginTitle(originTitle);
            // 第二个子类
            List<TPrediction> filteredPredictions2 = tPredictions.stream()
                    .filter(prediction -> prediction.getParentId().equals(tPrediction.getId()) && prediction.getPlaceName().equals(tRoadManageChildList.get(0).getPlace()))
                    .collect(Collectors.toList());

            Map<Long, List<TPrediction>> groupedPredictions1 = filteredPredictions2.stream()
                    .collect(Collectors.groupingBy(TPrediction::getFt));

            // 第三个子类
            List<TPrediction> filteredPredictions3 = tPredictions.stream()
                    .filter(prediction -> prediction.getParentId().equals(tPrediction.getId()) && prediction.getPlaceName().equals(tRoadManageChildList.get(1).getPlace()))
                    .collect(Collectors.toList());

            Map<Long, List<TPrediction>> groupedPredictions2 = filteredPredictions3.stream()
                    .collect(Collectors.groupingBy(TPrediction::getFt));


            List<OriginVo> originData = new ArrayList<>();

            for (TPrediction filteredPrediction : filteredPredictions) {
                Date localTime = filteredPrediction.getLocalTime();
                long localTimeInMillis = localTime.getTime();
                long newTimeInMillis = localTimeInMillis + (filteredPrediction.getFt() * 60 * 60 * 1000);
                Date evenStartTime = new Date(newTimeInMillis);
                int hours = evenStartTime.getHours();
                transTitle.add(hours +"点");

                // 封装表格中的数据
                OriginVo originVo = new OriginVo();
                // originVo.setSnow(filteredPrediction.getSnow());
                originVo.setRain(filteredPrediction.getRain());
                originVo.setTmp1(groupedPredictions1.get(filteredPrediction.getFt()).get(0).getAirtmp());
                originVo.setTmp2(groupedPredictions2.get(filteredPrediction.getFt()).get(0).getAirtmp());
                originData.add(originVo);

            }

            predictionTable.setOriginData(originData);
            predictionTable.setTransTitle(transTitle);
            predictionVo.setPredictionTable(predictionTable);
            predictionVoList.add(predictionVo);
        }

        predictionMap.put("predictionVo", predictionVoList);


        return predictionMap;
    }


    /**
     *
     * @param queryTime
     * @return
     */
    @Override
    public String release(String queryTime) {
        return null;
    }
}
